package EstimatorPackage;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author Martin Haulrich
 */
public class EmpSuffix {

     Map<String, Integer> counts;

    public int diffSuf;

    public int totalSuf;

    public int wordsCount;

    public EmpSuffix() {
    }

    public EmpSuffix(List<String> words) {
        Set<String> dw = new HashSet<String>();
        counts = new HashMap<String, Integer>();
        int t = 0;
        for (String w : words) {

            dw.add(w);
            for (int i = 1; i <= w.length(); i++) {
                String suf = w.substring(w.length() - i);
                Integer c = counts.get(suf);
                if (c == null) {
                    c = 1;
                } else {
                    c++;
                }
                counts.put(suf, c);
                t++;
            }
        }
        diffSuf = counts.size();
        //diffSuf = dw.size();
        totalSuf = t;
        wordsCount = words.size();
  //      System.err.println(counts);
    }

    public int getCount(String s) {

        Integer c = counts.get(s);
        if (c == null) {
 //           System.err.println("IMMPPP");
            return 0;
        }
        //  System.err.println("    count: " + c);
        return  c;

    }

    private double getLaplaceCount(String s) {
        double lc = (double) (getCount(s) + 1)  / (totalSuf + diffSuf);
        //   System.err.println("    lapcount: " + lc);
        return lc;
    }

    public double getEstimate(String s) {
        //    System.err.println(s);
        if (s.isEmpty()) {
            return 0;
        }
        int c = getCount(s);
//        if (c==0) {
////            System.err.println("Returning 0 for: " + s);
//            return Consts.logZero;
//        }
        //double c = (double) getLaplaceCount(s);


        double e = Math.log10((double) c / ((double) wordsCount));
//           System.err.println("    e: " + e);
        if (e > 0) {
            System.err.println("????????????????? " + s + " = " + e);
        }
        return e;
    }
    

}
